package com.wangzhixuan.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.result.PageInfo;
import com.wangzhixuan.commons.utils.StringUtils;
import com.wangzhixuan.model.TDatadic;
import com.wangzhixuan.model.vo.TDatadicVo;
import com.wangzhixuan.service.ITDatadicService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author thomas_liu
 * @since 2018-07-21
 */
@Controller
@RequestMapping("/tDatadic")
public class TDatadicController extends BaseController {

    @Autowired 
    private ITDatadicService tDatadicService;
    
    @GetMapping("/manager")
    public String manager() {
        return "admin/tDatadic/tDatadicList";
    }
    
    @PostMapping("/dataGrid")
    @ResponseBody
    public PageInfo dataGrid(TDatadicVo tDatadicVo, Integer page, Integer rows, String sort,String order) {
        PageInfo pageInfo = new PageInfo(page, rows, sort, order);
        Map<String,Object> condition = new HashMap<String,Object>();
        if(StringUtils.isNotBlank(tDatadicVo.getDatadicName())){
        	condition.put("datadicName", tDatadicVo.getDatadicName());
        }
        if(StringUtils.isNotBlank(tDatadicVo.getDatadicValue())){
        	condition.put("datadicValue", tDatadicVo.getDatadicValue());
        }
        if (tDatadicVo.getCreatedateStart() != null) {
            condition.put("startTime", tDatadicVo.getCreatedateStart());
        }
        if (tDatadicVo.getCreatedateEnd() != null) {
            condition.put("endTime", tDatadicVo.getCreatedateEnd());
        }
        pageInfo.setCondition(condition);
        tDatadicService.selectDataGrid(pageInfo);
        return pageInfo;
    }
    
    /**
     * 添加页面
     * @return
     */
    @GetMapping("/addPage")
    public String addPage() {
        return "admin/tDatadic/tDatadicAdd";
    }
    
    /**
     * 添加
     * @param 
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Object add(@Valid TDatadic tDatadic) {
        tDatadic.setCreateTime(new Date());
//        tDatadic.setUpdateTime(new Date());
//        tDatadic.setDeleteFlag(0);
        boolean b = tDatadicService.insert(tDatadic);
        if (b) {
            return renderSuccess("添加成功！");
        } else {
            return renderError("添加失败！");
        }
    }
    
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public Object delete(Integer id) {
        TDatadic tDatadic = new TDatadic();
        tDatadic.setId(id);
        tDatadic.setCreateTime(new Date());
//        tDatadic.setDeleteFlag(1);
        boolean b = tDatadicService.updateById(tDatadic);
        if (b) {
            return renderSuccess("删除成功！");
        } else {
            return renderError("删除失败！");
        }
    }
    
    /**
     * 编辑
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/editPage")
    public String editPage(Model model, Long id) {
        TDatadic tDatadic = tDatadicService.selectById(id);
        model.addAttribute("tDatadic", tDatadic);
        return "admin/tDatadic/tDatadicEdit";
    }
    
    /**
     * 编辑
     * @param 
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public Object edit(@Valid TDatadic tDatadic) {
        tDatadic.setCreateTime(new Date());
        boolean b = tDatadicService.updateById(tDatadic);
        if (b) {
            return renderSuccess("编辑成功！");
        } else {
            return renderError("编辑失败！");
        }
    }
}
